Thread: difference between **v and v[][]

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    11

    difference between **v and v[][]

    hi.
    i have a function like this
    Code:
    void print_matrix(int **t)
    when i call it with my dynamic matrix everything is ok.
    Code:
    int **Dmatrix;
    Dmatrix = malloc(N * sizeof(int*)); //N righe
    for (i = 0; i < N; i++) {
        Dmatrix[i] = malloc(N * sizeof(int));
    }
    print_matrix(Dmatrix);
    but if i call it with a static matrix i obtain a warning (and a segmentation foult )

    Code:
    int Smatrix[N][N];
    print_matrix(Smatrix);
    warning is:
    warning: passing argument 1 of ‘print_matrix’ from incompatible pointer type

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Difference between **v and v[][]

    int v[10][10] indicates 10 rows and 10 columns. But **v indicates multiple rows and columns. we can change that rows and columns any time.

    If we pass this to function it allocates based on the index. So it needs that much memory to store that array or pointer in a function.

    So if you pass with Smatrix[N][N] it needs N*N memory but in the pointer it is different. that is why it gives warning

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that when it is passed to a function as an argument, Smatrix, like any other array, is converted to a pointer to its first element. Therefore, what is passed is an int (*)[N], i.e., a pointer to an array of N ints. This is certainly different from an int**, i.e., a pointer to a pointer to an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    int v[10][10]; is stored in memory as 100 consecutive integers, which the compiler allows you to access two dimensionally by internally performing a simple stride calculation: index = y*10+x.

    int **v; is a pointer to 10 other pointers which each point to 10 ints (after the appropriate allocation). To get to any one of those ints requires following two pointers to where they point to.

    As you can see, they both require completely different work "under the hood". There is no way any compiler can treat these the same way because they're fundamentally very different.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think this illustrates the difference.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Not bad, though even those images don't do everything required to show how different they really are.
    For example, the arrow from the declaration to the first box in the second image indicates the following of a pointer, but the arrow in the first image is not, because in that case the array is the data and no pointers are involved at all. So that arrow should be removed.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This better?
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Great! I wasn't expecting you to spend any more effort to produce such a nice diagram, but hopefully the OP really gets it now
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Perhaps it can go into the FAQ somehow.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is the SourceForge Wiki. Feel free to create an article on the matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed